package com.fcscs.cservice.client.main.configuration.inroomdining;

import java.util.ArrayList;
import java.util.List;

import com.extjs.gxt.ui.client.Style.HorizontalAlignment;
import com.extjs.gxt.ui.client.event.IconButtonEvent;
import com.extjs.gxt.ui.client.event.Listener;
import com.extjs.gxt.ui.client.event.MessageBoxEvent;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.store.ListStore;
import com.extjs.gxt.ui.client.widget.Dialog;
import com.extjs.gxt.ui.client.widget.HorizontalPanel;
import com.extjs.gxt.ui.client.widget.MessageBox;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.grid.ColumnConfig;
import com.extjs.gxt.ui.client.widget.grid.ColumnData;
import com.extjs.gxt.ui.client.widget.grid.ColumnModel;
import com.extjs.gxt.ui.client.widget.grid.Grid;
import com.extjs.gxt.ui.client.widget.grid.GridCellRenderer;
import com.fcscs.core.gwt.common.CoreConstants;
import com.fcscs.core.gwt.common.CoreEnums.SubmitType;
import com.fcscs.core.gwt.common.CoreUtil;
import com.fcscs.core.gwt.common.FormSubmitListener;
import com.fcscs.core.gwt.common.ui.CenteredInfo;
import com.fcscs.core.gwt.common.ui.FcsFields.HighLightIconButton;
import com.fcscs.core.gwt.common.ui.FcsFormPanel;
import com.fcscs.core.gwt.common.ui.PagingSearchResultsPanel.ModelSelectListener;
import com.fcscs.core.gwt.common.ui.SimpleGridPanel;
import com.fcscs.cservice.client.common.CServiceConstants;
import com.fcscs.cservice.client.common.model.configuration.inroomdining.ChargeType;
import com.fcscs.cservice.client.common.model.configuration.inroomdining.ChargeTypeModelData;
import com.fcscs.share.gwt.common.ShareGWTConstants;

public class ChargeTypeGrid extends SimpleGridPanel<ChargeTypeModelData> implements
		FormSubmitListener<ChargeType>, ModelSelectListener<ChargeTypeModelData> {
	private static ChargeTypeGrid INSTANCE;
	private ChargeTypeDialog chargeTypeDialog;

	public ChargeTypeGrid(String heading, ModelSelectListener<ChargeTypeModelData> l) {
		super(heading, l, (Object) null);
		setCollapsible(false);
		registerListeners(this);
		setSize(650, 520);
	}

	public ChargeTypeGrid getInstance(String title,
			ModelSelectListener<ChargeTypeModelData> modelSelectListener) {
		if (null == INSTANCE) {
			INSTANCE = new ChargeTypeGrid(title, modelSelectListener);
		}

		return INSTANCE;
	}

	@Override
	public void onModelClick(ChargeTypeModelData model) {

	}

	@Override
	public void onModelDoubleClick(ChargeTypeModelData model) {

	}

	@Override
	public void onModelEnterPress(ChargeTypeModelData model) {

	}

	@Override
	public void formSubmitCallBack(FcsFormPanel<ChargeType> form, SubmitType submitType) {

	}

	@Override
	protected ColumnModel createColumnConfig(Object... params) {
		List<ColumnConfig> columns = new ArrayList<ColumnConfig>();

		ColumnConfig cc = CoreUtil.createNonSortableColumnConf(CoreConstants.FULLTNAME,
				CServiceConstants.CSERVICE_CONSTANTS.chargeType(), 160);
		columns.add(cc);
		
		columns.add(CoreUtil.createNonSortableColumnConf(CoreConstants.CODE,
				CServiceConstants.CSERVICE_CONSTANTS.activityBy(), 160));
		
		columns.add(CoreUtil.createNonSortableColumnConf(CoreConstants.CODE,
				CoreConstants.CORE_CONSTANTS.code(), 160));
		cc = CoreUtil.createNonSortableColumnConf(CoreConstants.ACTION,
				CoreConstants.CORE_CONSTANTS.action(), 160);
		cc.setRenderer(new GridCellRenderer<ChargeTypeModelData>() {
			public Object render(final ChargeTypeModelData model, String property, ColumnData config,
					int rowIndex, int colIndex, ListStore<ChargeTypeModelData> store,
					final Grid<ChargeTypeModelData> grid) {
				HorizontalPanel hr = new HorizontalPanel();
				hr.setHorizontalAlign(HorizontalAlignment.LEFT);

				HighLightIconButton ib = createEditBtn(model);
				HighLightIconButton id = createDeleteBtn(model, store);
				ib.setStyleAttribute("marginLeft", "3px");
				id.setStyleAttribute("marginLeft", "3px");
				ib.setId("PayTypeGrid_edit_button");
				id.setId("PayTypeGrid_dele_button");
				hr.add(ib);
				hr.add(id);
				return hr;
			}
		});
		columns.add(cc);
		ColumnModel columnModel = new ColumnModel(columns);
		return columnModel;
	}
	
	private HighLightIconButton createEditBtn(final ChargeTypeModelData model) {
		HighLightIconButton iconBtn = new HighLightIconButton(ShareGWTConstants.CSS_EDIT_SM_ICON,
				new SelectionListener<IconButtonEvent>() {
			@Override
			public void componentSelected(IconButtonEvent ce) {
				showEditDeviceDialog(model);
			}
		});
		iconBtn.setId("DeviceGridPanel_create_editbtn_id");
		iconBtn.setTitle(CoreConstants.CORE_CONSTANTS.edit());
		return iconBtn;
	}
	
	private void showEditDeviceDialog(ChargeTypeModelData model) {
		chargeTypeDialog = ChargeTypeDialog.getInstance(true);
		chargeTypeDialog.getFormPanel().updateForm(model.getEntity());
		chargeTypeDialog.show();
	}

	private HighLightIconButton createDeleteBtn(final ChargeTypeModelData model,final ListStore<ChargeTypeModelData> store) {
		HighLightIconButton deleBtn = new HighLightIconButton(CoreConstants.CSS_DELETE_SM_ICON,
				new SelectionListener<IconButtonEvent>() {
			@Override
			public void componentSelected(IconButtonEvent ce) {
				MessageBox.confirm(CoreConstants.CORE_MESSAGES.operation(CoreConstants.CORE_CONSTANTS.delete(), ShareGWTConstants.SHARE_GWT_CONSTANTS.device()),
						ShareGWTConstants.SHARE_GWT_MESSAGES.whetherOperation(CoreConstants.CORE_CONSTANTS.delete().toLowerCase(), model.getEntity().getChargetypeStr())
						,new Listener<MessageBoxEvent>() {

					@Override
					public void handleEvent(MessageBoxEvent be) {
						Button btn=be.getButtonClicked();
						if(Dialog.YES.equals(btn.getItemId())){
							store.remove(model);
							CenteredInfo.displayCompleted(CoreConstants.CORE_CONSTANTS.deleted());
						}
					}
				});
			}
		});
		deleBtn.setId("DeviceGridPanel_create_deleteBtn_id");
		deleBtn.setTitle(CoreConstants.CORE_CONSTANTS.delete());
		return deleBtn;
	}
}
